package com.esri.core.internal.tasks.ags.c;

import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.TaskListener;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.FeatureUtil;
import com.esri.core.map.Field;
import com.esri.core.map.Graphic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class f extends com.esri.core.internal.tasks.d<Map<Integer, FeatureSet>> {
    private static final long serialVersionUID = 1;

    public f(e eVar, String str, UserCredentials userCredentials) {
        super(eVar, str, userCredentials);
    }

    public f(e eVar, String str, UserCredentials userCredentials, TaskListener<Map<Integer, FeatureSet>> taskListener) {
        super(eVar, str, userCredentials, taskListener);
    }

    @Override // com.esri.core.internal.tasks.a
    /* renamed from: a */
    public Map<Integer, FeatureSet> execute() throws Exception {
        JsonParser jsonParser = null;
        try {
            jsonParser = com.esri.core.internal.io.handler.h.a(this.serviceURL + "/queryRelatedRecords", this.actionInput.generateRequestParams(), getServiceCredentials());
            return a(jsonParser);
        } finally {
            if (jsonParser != null) {
                jsonParser.close();
            }
        }
    }

    public static final Map<Integer, FeatureSet> a(JsonParser jsonParser) throws Exception {
        if (com.esri.core.internal.util.d.c(jsonParser)) {
            HashMap hashMap = null;
            SpatialReference spatialReference = null;
            Geometry.Type type = null;
            ArrayList arrayList = null;
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                String currentName = jsonParser.getCurrentName();
                jsonParser.nextToken();
                if ("spatialReference".equals(currentName)) {
                    spatialReference = SpatialReference.fromJson(jsonParser);
                } else if ("geometryType".equals(currentName)) {
                    type = com.esri.core.internal.util.d.b(jsonParser.getText());
                } else if ("fields".equals(currentName)) {
                    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                        arrayList = new ArrayList();
                        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                            arrayList.add(Field.fromJson(jsonParser));
                        }
                    }
                } else if ("relatedRecordGroups".equals(currentName)) {
                    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                            FeatureSet featureSet = null;
                            int i = -1;
                            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                                String currentName2 = jsonParser.getCurrentName();
                                jsonParser.nextToken();
                                if ("objectId".equals(currentName2)) {
                                    i = jsonParser.getIntValue();
                                } else if ("relatedRecords".equals(currentName2)) {
                                    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                                        ArrayList arrayList2 = new ArrayList();
                                        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                                            if (featureSet == null) {
                                                featureSet = new FeatureSet();
                                            }
                                            arrayList2.add(com.esri.core.internal.util.d.j(jsonParser));
                                        }
                                        if (featureSet != null) {
                                            if (arrayList2.size() == 0) {
                                                featureSet = null;
                                            } else {
                                                featureSet.setGraphics((Graphic[]) arrayList2.toArray(new Graphic[0]));
                                            }
                                        }
                                    }
                                } else {
                                    jsonParser.skipChildren();
                                }
                            }
                            if (i != -1) {
                                if (hashMap == null) {
                                    hashMap = new HashMap();
                                }
                                hashMap.put(Integer.valueOf(i), featureSet);
                            }
                        }
                    }
                } else {
                    jsonParser.skipChildren();
                }
            }
            if (hashMap != null && hashMap.size() > 0) {
                for (FeatureSet featureSet2 : hashMap.values()) {
                    if (featureSet2 != null) {
                        featureSet2.setSpatialReference(spatialReference);
                        FeatureUtil.updateFeatureSetGeometryType(featureSet2, type);
                        featureSet2.setFields(arrayList);
                    }
                }
            }
            return hashMap;
        }
        return null;
    }
}
